unit mainScreen;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Menus, PBCheckLB, workNode;

type
  TglobalView = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    PopupMenu1: TPopupMenu;
    UserDefined1: TMenuItem;
    ReferenceAnother1: TMenuItem;
    RuntimeInitialisation1: TMenuItem;
    LoadfromFile1: TMenuItem;
    Copy1: TMenuItem;
    Defaults1: TMenuItem;
    wnList: TPBCheckListBox;
    resList: TPBCheckListBox;
    passesList: TPBCheckListBox;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Exit1: TMenuItem;
    Settings1: TMenuItem;
    SubDomains1: TMenuItem;
    ConstBuffers1: TMenuItem;
    extures1: TMenuItem;
    Kernels1: TMenuItem;
    Help1: TMenuItem;
    Exit3: TMenuItem;
    WorkNodes1: TMenuItem;
    Resources1: TMenuItem;
    SaveDialog1: TSaveDialog;
    Passes1: TMenuItem;
    Open1: TMenuItem;
    WorkNodes2: TMenuItem;
    Passes2: TMenuItem;
    Resources2: TMenuItem;
    procedure passessListMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure resListMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ReferenceAnother1Click(Sender: TObject);
    procedure RuntimeInitialisation1Click(Sender: TObject);
    procedure LoadfromFile1Click(Sender: TObject);
    procedure Copy1Click(Sender: TObject);
    procedure Defaults1Click(Sender: TObject);
    procedure resListClick(Sender: TObject);
    procedure wnListMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure wnListClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Help1Click(Sender: TObject);
    procedure SubDomains1Click(Sender: TObject);
    procedure ConstBuffers1Click(Sender: TObject);
    procedure extures1Click(Sender: TObject);
    procedure Kernels1Click(Sender: TObject);
    procedure passesListMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure passesListClick(Sender: TObject);
    procedure Exit3Click(Sender: TObject);
    procedure WorkNodes1Click(Sender: TObject);
    procedure Resources1Click(Sender: TObject);
    procedure Passes1Click(Sender: TObject);
  private
    { Private declarations }
    //tmp : baseClassData;
    f : textFile;
  public
    { Public declarations }
  end;

var
  globalView: TglobalView;

implementation

uses newWorkNodeForm, globals, referenceAnotherForm, genericWNDataForm,
  newResourceForm, settingsForm, kernelSettingsForm, PassForm;

{$R *.dfm}

procedure TglobalView.passessListMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
RadioGroup1.ItemIndex := 1;
end;

procedure TglobalView.resListMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
RadioGroup1.ItemIndex := 2;
end;

procedure TglobalView.Button1Click(Sender: TObject);
begin
        case RadioGroup1.ItemIndex of
        0: newWNForm.ShowModal;
        1: passScreen.showModal;
        2: newresourceScreen.ShowModal;
        end;
end;

procedure TglobalView.FormCreate(Sender: TObject);
begin
doInitialise;
end;

procedure TglobalView.ReferenceAnother1Click(Sender: TObject);
begin
if(PopupMenu1.PopupComponent.Name = wnList.Name) then
  begin
    if wnList.ItemIndex > -1 then
      referenceScreen.myShow(WORK_NODE_TYPE);
  end
else if(PopupMenu1.PopupComponent.Name = passesList.Name) then
  begin
  end
else
  begin
    if resList.ItemIndex > -1 then
      referenceScreen.myShow(RESOURCE_TYPE);
  end;
end;

procedure TglobalView.RuntimeInitialisation1Click(Sender: TObject);
begin
if(PopupMenu1.PopupComponent.Name = wnList.Name) then
  begin
    if wnList.ItemIndex > -1 then
    genericWNDataScreen.myShow('Runtime Initialisation for Node: '+wnList.Items.Strings[wnList.ItemIndex],
                               'Runtime', 'N/A', WORK_NODE_TYPE);
  end
else if(PopupMenu1.PopupComponent.Name = passesList.Name) then
  begin
  end
else // resources List
  begin
    if resList.ItemIndex > -1 then
    genericWNDataScreen.myShow('Runtime Initialisation for Resource: '+resList.Items.Strings[resList.ItemIndex],
                               'Runtime', 'N/A', RESOURCE_TYPE);
  end;
end;

procedure TglobalView.LoadfromFile1Click(Sender: TObject);
begin
if(PopupMenu1.PopupComponent.Name = wnList.Name) then
  begin
     if wnList.ItemIndex > -1 then
        genericWNDataScreen.myShow('Runtime Initialisation for Node: '+wnList.Items.Strings[wnList.ItemIndex],
                                   'LoadFromFile', '', WORK_NODE_TYPE);
  end
else if(PopupMenu1.PopupComponent.Name = passesList.Name) then
  begin
  end
else
  begin
     if resList.ItemIndex > -1 then
        genericWNDataScreen.myShow('Runtime Initialisation for Resource: '+resList.Items.Strings[resList.ItemIndex],
                                   'LoadFromFile', '', RESOURCE_TYPE);
  end;
end;

procedure TglobalView.Copy1Click(Sender: TObject);
begin
if(PopupMenu1.PopupComponent.Name = wnList.Name) then
  begin
    if wnList.ItemIndex > -1 then
    genericWNDataScreen.myShow('Copy from another resource for Work Node: '+wnList.Items.Strings[wnList.ItemIndex],
                               'Copy', '',WORK_NODE_TYPE);
  end
else if(PopupMenu1.PopupComponent.Name = passesList.Name) then
  begin
  end
else
  begin
      if resList.ItemIndex > -1 then
      genericWNDataScreen.myShow('Copy from another resource for Resource: '+resList.Items.Strings[resList.ItemIndex],
                               'Copy', '',RESOURCE_TYPE);
  end;
end;

procedure TglobalView.Defaults1Click(Sender: TObject);
begin
if(PopupMenu1.PopupComponent.Name = wnList.Name) then
  begin
    if wnList.ItemIndex > -1 then
      begin
        baseClassData(WN_DataList.Items[wnList.ItemIndex]).set_instancingMethod('Default');
        baseClassData(WN_DataList.Items[wnList.ItemIndex]).set_imStringData('Default');
        baseClassData(WN_DataList.Items[wnList.ItemIndex]).set_passName('Default');
        baseClassData(WN_DataList.Items[wnList.ItemIndex]).set_iterations(0);
        wnListClick(nil);
      end;
  end
else if(PopupMenu1.PopupComponent.Name = passesList.Name) then
  begin
  end
else  // for resources list
  begin
    if resList.ItemIndex > -1 then
      begin

        baseClassData(Resources_DataList.Items[resList.ItemIndex]).set_instancingMethod('Default');
        baseClassData(Resources_DataList.Items[resList.ItemIndex]).set_imStringData('Default');
        baseClassData(Resources_DataList.Items[resList.ItemIndex]).set_passName('Default');
        baseClassData(Resources_DataList.Items[resList.ItemIndex]).set_iterations(0);
        resListClick(nil);
      end;
  end;
end;

procedure TglobalView.resListClick(Sender: TObject);
var
  i : Integer;
begin
  for i:= 0 to resList.Count-1 do
    begin
           if baseClassData(Resources_DataList.Items[i]).get_instancingMethod = '' then
              resList.State[i] := cbUnchecked
           else
              resList.State[i] := cbchecked;
    end;
end;

procedure TglobalView.wnListMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
RadioGroup1.ItemIndex := 0;
end;

procedure TglobalView.wnListClick(Sender: TObject);
var
  i : Integer;
begin
  for i:= 0 to wnList.Count-1 do
    begin
           if baseClassData(WN_DataList.Items[i]).get_instancingMethod = '' then
              wnList.State[i] := cbUnchecked
           else
              wnList.State[i] := cbchecked;
    end;
end;

procedure TglobalView.Button2Click(Sender: TObject);
var
  i : Integer;
  d : baseClassData;
  f : textFile;
begin
AssignFile(f, 'DataFile.txt');
ReWrite(f);

// printing the work node list first
WriteLn(f, 'WorkNode Database');
for i := 0 to WN_DataList.Count-1 do
   begin
     d := baseClassData(WN_DataList.Items[i]);
     WriteLn(f, 'Name: '+d.get_Name);
     WriteLn(f, 'Instancing: '+d.get_instancingMethod);
     WriteLn(f, 'IMStringdata: '+d.get_imStringData);
     WriteLn(f, 'PassName: '+d.get_passName);
     WriteLn(f, 'Iterations: '+ IntToStr(d.get_iterations));
     WriteLn(f, 'Data Type: '+  IntToStr(d.get_dataType));
     WriteLn(f, '============================================');
   end;


// printing the work node list first
WriteLn(f, 'Resource Database');
for i := 0 to WN_DataList.Count-1 do
   begin
     d := baseClassData(Resources_DataList.Items[i]);
     WriteLn(f, 'Name: '+d.get_Name);
     WriteLn(f, 'Instancing: '+d.get_instancingMethod);
     WriteLn(f, 'IMStringdata: '+d.get_imStringData);
     WriteLn(f, 'PassName: '+d.get_passName);
     WriteLn(f, 'Iterations: '+ IntToStr(d.get_iterations));
     WriteLn(f, 'Data Type: '+  IntToStr(d.get_dataType));
     WriteLn(f, '============================================');
   end;

CloseFile(f);
end;

procedure TglobalView.Help1Click(Sender: TObject);
begin
ShowMessage('RAD1.0 by Ali Ahmed ali.fci@gmail.com');
end;

procedure TglobalView.SubDomains1Click(Sender: TObject);
begin
settingsScreen.myShow(SUBDOMAIN_MODE);
end;

procedure TglobalView.ConstBuffers1Click(Sender: TObject);
begin
settingsScreen.myShow(CONSTBUFFER_MODE);
end;

procedure TglobalView.extures1Click(Sender: TObject);
begin
settingsScreen.myShow(TEXTURE_MODE);
end;

procedure TglobalView.Kernels1Click(Sender: TObject);
begin
kernelsettingsScreen.showModal;
end;

procedure TglobalView.passesListMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
RadioGroup1.ItemIndex := 1;
end;

procedure TglobalView.passesListClick(Sender: TObject);
var
  i : Integer;
begin
  for i:= 0 to passesList.Count-1 do
      passesList.State[i] := cbchecked
end;

procedure TglobalView.Exit3Click(Sender: TObject);
begin
close();
end;

procedure TglobalView.WorkNodes1Click(Sender: TObject);
var
  i : Integer;
  wn : baseClassData;
begin
SaveDialog1.FilterIndex := 1;
if SaveDialog1.Execute then
  begin
    assignFile(f, SaveDialog1.FileName);
    if FileExists(SaveDialog1.FileName) then
       append(f)
    else Rewrite(f);
    for i:=0 to WN_DataList.Count-1 do
      begin
        wn := baseClassData(WN_DataList.Items[i]);
        writeLn(f, wn.get_Name());
        writeLn(f, wn.get_instancingMethod());
        writeLn(f, wn.get_imStringData());
        writeLn(f, wn.get_passName());
        writeLn(f, wn.get_iterations());
        //writeLn(f, wn.get_dataType());
      end;
    WN_DataList.Clear;
    closeFile(f);
  end;
end;

procedure TglobalView.Resources1Click(Sender: TObject);
var
  i : Integer;
  wn : baseClassData;
begin
SaveDialog1.FilterIndex := 2;
if SaveDialog1.Execute then
  begin
    assignFile(f, SaveDialog1.FileName);
    if FileExists(SaveDialog1.FileName) then
       append(f)
    else Rewrite(f);
    for i:=0 to Resources_DataList.Count-1 do
      begin
        wn := baseClassData(Resources_DataList.Items[i]);
        writeLn(f, wn.get_Name());
        writeLn(f, wn.get_instancingMethod());
        writeLn(f, wn.get_imStringData());
        //writeLn(f, wn.get_dataType());
      end;
    Resources_DataList.Clear;
    closeFile(f);
  end;
end;

procedure TglobalView.Passes1Click(Sender: TObject);
begin
passScreen.Button10Click(Sender);
end;

end.
